$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyschoolbriefrenwu/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 20, key: true },
			{ label: '人物图片', name: 'renwuUrl', index: 'renwu_url', width: 60,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.renwuUrl!=null&&rowdata.renwuUrl!=''){
                        return '<img src='+rowdata.renwuUrl+' width="50" height="50"/>';
                    }else{
                        return '';
                    }


                } },
			{ label: '人物标签', name: 'renwuLable', index: 'renwu_lable', width: 130 },
			{ label: '学校', name: 'schoolId', index: 'school_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var schoolname = '' ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/fyschool/info/"+rowdata.schoolId,
                        success: function(r){
                            schoolname = r.fySchool.tname;

                        }
                    });
                    return schoolname;
                } },
			{ label: '添加时间', name: 'creattime', index: 'creattime', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.querySchool();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            shcoolid:null
        },
        fileList1: [],
		showList: true,
		title: null,
		fySchoolBriefRenwu: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fySchoolBriefRenwu = {};
            vm.fileList1 = [];
			vm.getSchool("add");
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getSchool("update");
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.fySchoolBriefRenwu.id == null ? "sys/fyschoolbriefrenwu/save" : "sys/fyschoolbriefrenwu/update";
            vm.fySchoolBriefRenwu.renwuUrl = '';
            if(vm.fileList1.length>0){
                vm.fySchoolBriefRenwu.renwuUrl = vm.fileList1[0].url;
            }
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fySchoolBriefRenwu),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyschoolbriefrenwu/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        handleRemove1: function(file, fileList) {
            console.log(fileList);
            vm.fileList1 = [];
            if(fileList.length>0){
                for(var i=0;i<fileList.length;i++){
                    vm.fileList1.push({name: fileList[i].name,url: fileList[i].url});
                }
            }
            console.log(vm.fileList1);

        },
        handlePreview: function(file) {
            console.log(file);
        },

        handsuccess1: function(response, file, fileList) {
            vm.fileList1.push({name: response.url,url: response.url});
        },
		getInfo: function(id){
			$.get(baseURL + "sys/fyschoolbriefrenwu/info/"+id, function(r){
                vm.fySchoolBriefRenwu = r.fySchoolBriefRenwu;

                vm.fileList1 = [];
                if(r.fySchoolBriefRenwu.renwuUrl!=null&&r.fySchoolBriefRenwu.renwuUrl!=''){
                    vm.fileList1.push({name: r.fySchoolBriefRenwu.renwuUrl,url: r.fySchoolBriefRenwu.renwuUrl});
                }
            });
		},
        getSchool: function(mn){
            $("#schoolId").html("");
            $.get(baseURL + "sys/fyschool/alllist", function(r){
                var temp;
                $.each(r.alllist, function (i) {
                    if(mn =='add' && i ==0){
                        vm.fySchoolBriefRenwu.schoolId = r.alllist[i].schoolId;
                    }
                    temp +="<option value='"+r.alllist[i].schoolId+"'>"+r.alllist[i].tname+"</option>";

                });
                $("#schoolId").append(temp);

            });
        },
        querySchool: function(){
            $("#shcool_id").html("");
            $.get(baseURL + "sys/fyschool/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {
                    temp +="<option value='"+r.alllist[i].schoolId+"'>"+r.alllist[i].tname+"</option>";

                });
                $("#shcool_id").append(temp);

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'shcoolid': vm.q.shcoolid},
                page:page
            }).trigger("reloadGrid");
		}
	}
});